package com.groupbaby.ydd.controller;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.groupbaby.ydd.common.ResultData;
import com.groupbaby.ydd.entity.YddAdvpos;
import com.groupbaby.ydd.service.IYddAdvposService;
import com.groupbaby.ydd.utils.GetPage;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author author
 * @since 2022-05-22
 */

@Slf4j
@RestController
@RequestMapping("/ydd_advpos")
public class YddAdvposController {

    @Autowired
    private IYddAdvposService iYddAdvposService;

    /**
     * 分页
     *
     * @param advposdesc
     * @param advposname
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/list")
    public ResultData page(String advposdesc, String advposname, String page, String limit) {

        Long Page = Long.parseLong(page);
        Long Limit = Long.parseLong(limit);

        //分页构造器
        Page<YddAdvpos> pageInfo = new Page(Page, Limit);
        //条件构造器
        LambdaQueryWrapper<YddAdvpos> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(advposdesc), YddAdvpos::getAdvposdesc, advposdesc);
        queryWrapper.like(StringUtils.isNotEmpty(advposname), YddAdvpos::getAdvposname, advposname);

        Page<YddAdvpos> avdPage = iYddAdvposService.page(pageInfo, queryWrapper);

        Long count = Long.valueOf(iYddAdvposService.count(queryWrapper));

        List<YddAdvpos> records = avdPage.getRecords();

//        GetPage getPage = GetPage.init(count, Page, Limit, records);
        com.groupbaby.ydd.common.Page getPage = com.groupbaby.ydd.common.
                Page.init(count.intValue(),Page.intValue(),  Limit.intValue(), records);
        return ResultData.ok("", getPage);
    }

    /**
     * 回显数据
     *
     * @param id
     * @return
     */
    @GetMapping("/rowInfo")
    public ResultData get(String id) {

        YddAdvpos byId = iYddAdvposService.getById(id);

        return ResultData.ok("", byId);
    }


    /**
     * 删除
     *
     * @param id
     * @return
     */
    @GetMapping("/del")
    public ResultData del(String id) {
        iYddAdvposService.removeById(id);
        return ResultData.ok("", "删除成功");
    }

    /**
     * 修改
     *
     * @param yddAdvpos
     * @return
     */
    @PostMapping("update")
    public ResultData update(@RequestBody YddAdvpos yddAdvpos) {
        iYddAdvposService.updateById(yddAdvpos);
        return ResultData.ok("", "修改成功");
    }

    /**
     * 添加
     *
     * @param yddAdvpos
     * @return
     */
    @PostMapping("/add")
    public ResultData save(@RequestBody YddAdvpos yddAdvpos) {
        iYddAdvposService.save(yddAdvpos);
        return ResultData.ok("", "添加成功");
    }

    /**
     * 广告名称
     * @return
     */
    @GetMapping("/all")
    public ResultData all(){
        List<YddAdvpos> list = iYddAdvposService.getAll();
        return ResultData.ok("",list);
    }
}
